﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public partial class BossListScene : Scene
{
	const string DEFAULT_BOSS_LIST_CELL_NAME = "Cell - Default Boss List";

	double serverTime;

	Dictionary<int, RaidBoss> defaultBossList = new Dictionary<int, RaidBoss>();
	Dictionary<int, GameObject> defaultBossObjectList = new Dictionary<int, GameObject>();
	Dictionary<int, string[]> defaultBossRewardList = new Dictionary<int, string[]>();
	
	public BossListTabBar defaultBossListTabBar;
	public GameObject defaultBossListCell;
	public UIScrollView defaultBossListScrollView;
	public UIGrid defaultBossListGrid;

	public GameObject rewardGroup;
	
	void LoadDefaultBossList()
	{
		NetworkManager.Instance.C2S_GET_SERVERTIME((responseDataWrapper) => {

			serverTime = responseDataWrapper.data;

			NetworkManager.Instance.C2S_RAIDBOSS_LIST(1, (responseData) => {

				if (responseData.ecode != 0)
				{
					PopupUtility.OneButtonErrorPopup(responseData.ecode);
					return;
				}
				
				Debug.Log ("LoadDefaultBossList - OnSuccess");
				
				GameObject g = null;
				int idx = 0;
				
				RemoveAllDefaultBossList();
				
				if (responseData.data.Count <= 0)
				{
					#region - GStar Version
					UserData.Instance.isExistRaidBoss = false;
					#endregion
					DeactivateRaidBossModel();
					NGUITools.SetActive(defaultBossBlankView, true);
					return;
				}

				#region - GStar Version
				UserData.Instance.isExistRaidBoss = true;
				#endregion
				NGUITools.SetActive(defaultBossBlankView, false);
				
				foreach (RaidBoss rb in responseData.data)
				{
					AddRaidBossModelObject(rb.raidBossId);

					g = NGUITools.AddChild(defaultBossListGrid.transform.gameObject, defaultBossListCell);
					g.transform.localScale = Vector3.one;
					g.name = DEFAULT_BOSS_LIST_CELL_NAME;
					
					DefaultBossListCell dbc = g.GetComponent<DefaultBossListCell>();
					dbc.indexPath = idx;
					dbc.bossNameLabel.text = string.Format( "Lv{0} {1}", rb.lv, Localization.Get("bossName" + rb.raidBossId.ToString()) );
					dbc.finderNameLabel.text = rb.name;
					dbc.hpSlider.value = 0.6f;

					float currentHp = (float)rb.hp;
					float baseHp = (float)bossDataDictionary[rb.raidBossId].baseHP;
					float growLevelHp = (rb.lv - 1) * (float)bossDataDictionary[rb.raidBossId].growHP;
					dbc.hpSlider.value = currentHp / (baseHp + growLevelHp);

					dbc.remainTimeLabel.text = DatetimeToFomattedString(rb.endDate);

					#region - GStar Version
					dbc.remainTimeLabel.gameObject.SetActive(false);
					//dbc.remainTimeLabel.text = DatetimeToFomattedString(rb.endDate);
					//StartCoroutine(dbc.RemainTimeTimer(rb.endDate));
					#endregion

					string bossRewardCharTemplate = Localization.Get ("bossRewardChar");
					
					string[] rewardArray = new string[3];
					
					foreach (DBossReward each in bossReward)
					{
						// 임시 : 중복되는 값인 오류 때문에 임시로 처리함
						if (defaultBossRewardList.ContainsKey(idx))
							continue;

						if (each.bossId == rb.raidBossId && each.bosslevelMin <= rb.lv && each.bosslevelMax >= rb.lv)
						{
							rewardArray[0] = string.Format(Localization.Get("uiCommonTextKina2"), each.findKina);
							rewardArray[1] = string.Format(bossRewardCharTemplate, Localization.Get ("trait" + bossCharTrait[each.joinCharRewardId - 1].traitId)) + " x" + each.value1.ToString();
							rewardArray[2] = string.Format(bossRewardCharTemplate, Localization.Get ("trait" + bossCharTrait[each.suppressCharRewardId - 1].traitId)) + " x" + each.value2.ToString();
							defaultBossRewardList.Add(idx, rewardArray);
						}
					}
					
					defaultBossObjectList.Add (idx, g);
					defaultBossList.Add (idx, rb);
					
					idx++;
				}

				defaultBossListGrid.Reposition();
				defaultBossListScrollView.ResetPosition();
				defaultBossListTabBar.Init();
			});
		});
	}

	void RemoveAllDefaultBossList()
	{
		defaultBossList.Clear();
		
		foreach (KeyValuePair<int, GameObject> pair in defaultBossObjectList)
		{
			NGUITools.SetActive(pair.Value, false);
			NGUITools.Destroy(pair.Value);
		}
		defaultBossObjectList.Clear();
	}
}
